import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class MessageService {
  constructor() { }

  openDevTools(tabId?:number) {
    if(!tabId) {
        return;
      }
  
      chrome.runtime.sendMessage({ action: "openDevTools", tabId: tabId });
  }

  listenToContextMenuMessage() {
    chrome.contextMenus.onClicked.addListener((info, tab) => {
        if (info.menuItemId === 'ias_dev_tools') {
          chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
            const currentTab = tabs[0];
  
            if (currentTab) {
              this.openDevTools(currentTab.id);
            }
          });
        }
      });
  }
}
